<?php

namespace app\model;

use app\model\fuhai\MarktingOrder;
use think\Model;
use think\model\relation\HasOne;

class CouponOrder extends Model
{
    public function pageData($type = 2): array
    {
        $param = request()->param();
        $account = request()->admin;
        $data = $this->when($type == 1, function ($query) use ($account) {
            if ($account['type'] == 1) {
                $query->where('supplier_id', $account['supplier_id']);
            } else {
                $query->where('store_id', $account['store_id']);
            }
        })->when(!empty($param['create_time']), function ($query) use ($param) {
            $query->whereBetweenTime('create_time', $param['create_time'][0] . " 00:00:00", $param['create_time'][1] . " 23:59:59");
        })->when(!empty($param['product_id']), function ($query) use ($param) {
            $query->where('product_id', $param['product_id']);
        })->when(!empty($param['mobile']), function ($query) use ($param) {
            $query->where('mobile', $param['mobile']);
        })->when(!empty($param['car']), function ($query) use ($param) {
            $query->where('car', $param['car']);
        })->with(['adminInf' => function ($query) {
            $query->with(['store', 'supplier']);
        }])->when(!empty($param['store_id']), function ($query) use ($param) {
            $query->where('store_id', $param['store_id']);
        })->when(!empty($param['supplier_id']), function ($query) use ($param) {
            $query->where('supplier_id', $param['supplier_id']);
        })->when(!empty($param['state']), function ($query) use ($param) {
            $query->where('state', $param['state']);
        })->order('create_time', 'desc')->paginate([
            "list_rows" => $param['limit'] ?? 30,
            "page" => $param['page'] ?? 1
        ])->toArray();
        return ['lst' => $data['data'], 'total' => $data['total'], 'totalPage' => $data['last_page']];
    }

    public function adminInf(): HasOne
    {
        return $this->hasOne(Account::class, "id", "admin");
    }

    public function download($type = 2)
    {
        $data = $this->pageData($type);
        $state = [
            '确认中',
            '已发放',
            '发放失败',
            '系统取消',
            '退单'
        ];
        $result = array_map(function ($item) use ($state) {
//            $coupon = json_decode($item['']);
            return [
                $item['order_sn'],
                $item['payment_sn'],
                $item['name'],
                $item['mobile'],
                $item['adminInf']['name'],
                $item['adminInf']['store']['name'],
                $item['car'],
                $item['remark'],
                $state[$item['state']],
                $item['create_time']
            ];
        }, $data['lst']);
        $totalPage = $data['totalPage'];
        $header = [
            '订单编号',
            '支付单号',
            '姓名',
            '手机号',
            '操作人',
            '门店名称',
            '车牌号',
            '备注',
            '状态',
            '时间'
        ];
        $title = ["卡券发放记录"];
        $file = "卡券发放记录";
        return compact("result", "header", "title", "totalPage", "file");
    }

    public function thirdCoupon(): HasOne
    {
        return $this->hasOne(MarktingOrder::class, "order_sn", "payment_sn");
    }

    public function couponOrder()
    {
        return $this->hasMany(\app\model\fuhai\CouponOrder::class, "supplier_order", "order_sn")->with(['couponIts']);
    }
}
